//
//  DHNetworkManager.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/4/27.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHNetworkManager.h"
#import <AFHTTPSessionManager.h>


//网络工具类单例
@implementation DHNetworkManager

+ (instancetype)shareManager{
    
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DHNetworkManager alloc] init];
        
       [(DHNetworkManager *)instance responseSerializer].acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json", nil];
        
    });
    return instance;
}
/*
 用一个函数网络封装请求GET、POST的方法
 **/
- (void)requestWithMethod:(MethodHTTP)method UrlString:(NSString *)urlStr params:(NSDictionary *)params completion:(void(^)(id json ,BOOL isSuccess))completion{
    
    // 成功回调
    void(^successful)(id, NSURLSessionDataTask *) = ^(id response, NSURLSessionDataTask *task){
        
        completion(task,true);
    };
    
    // 是失败回调
    void(^requestFailure)(NSURLSessionDataTask *, NSError *) = ^(NSURLSessionDataTask *task, NSError *error){
        completion(nil,false);
        
        //FIXME: 网络请求失败
        NSLog(@"失败---%@",error);
    };
    
    
    if (method == MethodGet) {
    
        [self GET:urlStr parameters:params progress:nil success:successful failure:requestFailure];
        
    }else{
        
        [self POST:urlStr parameters:params progress:nil success:successful failure:requestFailure];
    }
    
}

@end
